﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WEB.Core.Controls;
using Ext.Net;
using WEB.Core.Util;
using log4net;
using WEB.BLL.Services;
using WEB.BLL.Entities;
using WEB.BLL;

namespace WEB.APP.Modules.System
{
    public partial class User : BasePage, IBaseListPage
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(User));
        public User()
            : base(SCREENS.USER)
        {

        }

        protected override void Page_Load(object sender, EventArgs e)
        {
            this.WindowEditor.SaveClick += new EventHandler(WindowEditor_SaveClick);
            base.Page_Load(sender, e);
        }

        [DirectMethod]
        public void DocumentReady()
        {
        }
        void WindowEditor_SaveClick(object sender, EventArgs e)
        {
            this.storeMain.DataBind();
        }
        protected void storeMain_OnRefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            UserEntity userE = new UserEntity();
            int start = Util.Obj2Int(e.Parameters["start"]);
            int endNum = Util.Obj2Int(e.Parameters["limit"]);
            endNum += start;
            userE.Keyword = txtKeyword.Text;
            userE.Start = start;
            userE.EndNum = endNum;
            userE.TotalRow = 0;
            UserService userS = UserService.GetInstance();
            IList<UserEntity> lstUser = userS.Find(userE);
            if (null != lstUser)
            {
                this.storeMain.DataSource = lstUser;
                this.storeMain.DataBind();
                e.Total = userE.TotalRow;
            }
        }
        protected void grdMain_OnCommand(object sender, DirectEventArgs e)
        {
            string id = e.ExtraParams["id"];
            string command = e.ExtraParams["command"];
            if (IsEdit && Constants.Edit.Equals(command))
            {
                this.WindowEditor.Show(id,true);
            }
            else if (IsDelete && Constants.Delete.Equals(command))
            {
                UserEntity userE = new UserEntity();
                UserService userS = UserService.GetInstance();
                userE.UserName = id;
                userS.Delete(userE);
                this.storeMain.DataBind();
            }

        }
        public void btnAdd_Click(object sender, DirectEventArgs e)
        {
            this.WindowEditor.Show();
        }
        public void btnEdit_Click(object sender, DirectEventArgs e) { }
        public void btnDelete_Click(object sender, DirectEventArgs e) { }
        public void btnPrint_Click(object sender, DirectEventArgs e) { }
        public void btnImport_Click(object sender, DirectEventArgs e) { }
        public void btnExport_Click(object sender, DirectEventArgs e) { }
        public void btnFind_Click(object sender, DirectEventArgs e) { }

        [DirectMethod]
        public void Edit(string UserName)
        {
            WindowEditor.Show(UserName, IsEdit);
        }
        [DirectMethod]
        public void Delete(string UserName)
        {
            try
            {
                if (!string.IsNullOrEmpty(UserName))
                {
                    UserEntity UserE = new UserEntity();
                    UserService UserS = UserService.GetInstance();
                    UserE.UserName = UserName;
                    UserE.CreatedBy = UserE.UpdatedBy = WEBContext.Current.User.UserName;
                    UserS.Delete(UserE);
                    this.storeMain.DataBind();
                }
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        [DirectMethod]
        public void grdMain_AfterEdit(string username, bool value)
        {
            UserEntity userE = new UserEntity();
            UserService userS = UserService.GetInstance();
            userE.UserName = username;
            userE.IsActive = value;
            userS.UpdateCheck(userE);
        }
    }
}
